﻿/*******************************
 * Developer: Joao Pedro Palma *
 * Empresa: Anaprog            *
 * Data: 05-01-2010            *
 * Hora: 17:48                 *
 *******************************/

using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using APdal;

namespace Contactos
{
    public partial class frmPrincipal : Form
    {
        #region "Declarações"

        private DataSet _ds;
        private DataView _dvContactos, _dvTecnicos;
        private DataTable _dtContactos, _dtTecnicos;
        private SqlDataAdapter _daContactos, _daTecnicos;
        private SqlCommandBuilder _builder;

        #endregion

        public frmPrincipal()
        {
            InitializeComponent();
            Text = Application.ProductName + " " + Application.ProductVersion;
            SetStyle();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataLink dll = new dataLink();
            _conn = dataLink.conn;

            try
            { _conn.Open(); }
            catch
            {
                frmSql sql = new frmSql();
                sql.ShowDialog(this);
            }

            _ds = new DataSet("ListaContactos") { EnforceConstraints = false };
            _daContactos = new SqlDataAdapter("Select * from CONTACTOS", _conn);
            _daContactos.SelectCommand.Connection = _conn;
            _daTecnicos = new SqlDataAdapter("Select * from Tecnicos", _conn);

            _dtContactos = new DataTable();
            _dtTecnicos = new DataTable();

            _ds.Tables.Add(_dtContactos);
            _ds.Tables.Add(_dtTecnicos);

            _daContactos.Fill(_dtContactos);
            _daTecnicos.Fill(_dtTecnicos);

            _dvContactos = new DataView(_dtContactos);
            _dvTecnicos = new DataView(_dtTecnicos);

            _builder = new SqlCommandBuilder(_daContactos);
            _daContactos.InsertCommand = _builder.GetInsertCommand();

            GridContactos.DataSource = _dvContactos;
            GridContactos.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            GridContactos.Columns["Nome"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            GridContactos.Columns["Numero"].Visible = false;
            GridContactos.Columns["Telefone"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }

        #region Filtro por Nome

        private void txtNome_KeyDown(object sender, KeyEventArgs e)
        { _dvContactos.RowFilter = "Nome like '%" + txtNome.Text + "%'"; }

        #endregion

        public void SetStyle()
        {
            BackColor = Color.FromArgb(228, 234, 230);
            SetBase(228, 234, 230, Color.FromArgb(254, 209, 94));
        }

        #region SetBase

        public void SetBase(int R, int G, int B, Color HaloColor)
        {
            this.SuspendLayout();
            foreach (Control control in this.Controls)
            {
                if (typeof(RibbonStyle.TabStrip) == control.GetType())
                {
                    ((RibbonStyle.TabStripProfessionalRenderer)((RibbonStyle.TabStrip)control).Renderer).HaloColor = HaloColor;
                    ((RibbonStyle.TabStripProfessionalRenderer)((RibbonStyle.TabStrip)control).Renderer).BaseColor = Color.FromArgb(R + 4, G + 3, B + 3);
                    for (int i = 0; i < ((RibbonStyle.TabStrip)control).Items.Count; i++)
                    {
                        RibbonStyle.Tab _tab = (RibbonStyle.Tab)((RibbonStyle.TabStrip)control).Items[i];

                        #region Set Tab Colors
                        if (Color.FromArgb(R, G, B).GetBrightness() < 0.5)
                        {
                            try
                            {
                                _tab.ForeColor = Color.FromArgb(R + 76, G + 71, B + 66);
                            }
                            catch
                            {
                                _tab.ForeColor = Color.FromArgb(250, 250, 250);
                            }
                        }
                        else
                        {
                            try
                            {
                                _tab.ForeColor = Color.FromArgb(R - 96, G - 91, B - 86);
                            }
                            catch
                            {
                                _tab.ForeColor = Color.FromArgb(10, 10, 10);
                            }
                        }
                        #endregion
                    }

                    control.BackColor = Color.FromArgb(R - 10, G - 8, B + 13);

                }
                if (typeof(RibbonStyle.TabPageSwitcher) == control.GetType())
                {
                    control.BackColor = Color.FromArgb(R - 10, G - 8, B + 13);

                    foreach (Control _control in control.Controls)
                    {
                        if (typeof(RibbonStyle.TabStripPage) == _control.GetType())
                        {
                            ((RibbonStyle.TabStripPage)_control).BaseColor = Color.FromArgb(R, G, B);
                            ((RibbonStyle.TabStripPage)_control).BaseColorOn = Color.FromArgb(R, G, B);

                            foreach (Control __control in _control.Controls)
                            {
                                if (typeof(RibbonStyle.TabPanel) == __control.GetType())
                                {
                                    #region Set TabPanel Colors
                                    if (Color.FromArgb(R, G, B).GetBrightness() < 0.5)
                                    {
                                        try
                                        {
                                            __control.ForeColor = Color.FromArgb(R + 76, G + 71, B + 66);
                                        }
                                        catch
                                        {
                                            __control.ForeColor = Color.FromArgb(250, 250, 250);
                                        }
                                    }
                                    else
                                    {
                                        try
                                        {
                                            __control.ForeColor = Color.FromArgb(R - 96, G - 91, B - 86);
                                        }
                                        catch
                                        {
                                            __control.ForeColor = Color.FromArgb(10, 10, 10);
                                        }
                                    }
                                    #endregion

                                    ((RibbonStyle.TabPanel)__control).BaseColor = Color.FromArgb(R, G, B);
                                    ((RibbonStyle.TabPanel)__control).BaseColorOn = Color.FromArgb(R + 6, G + 6, B + 10);

                                    foreach (Control ___control in __control.Controls)
                                    {
                                        if (typeof(RibbonStyle.RibbonButton) == ___control.GetType())
                                        {
                                            ((RibbonStyle.RibbonButton)___control).InfoColor = Color.FromArgb(R, G, B);

                                            RibbonStyle.RibbonButton _but = (RibbonStyle.RibbonButton)___control;

                                            #region Set Button Colors
                                            if (Color.FromArgb(R, G, B).GetBrightness() < 0.5)
                                            {
                                                try
                                                {
                                                    _but.ForeColor = Color.FromArgb(R + 76, G + 71, B + 66);
                                                }
                                                catch
                                                {
                                                    _but.ForeColor = Color.FromArgb(250, 250, 250);
                                                }
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    _but.ForeColor = Color.FromArgb(R - 96, G - 91, B - 86);
                                                }
                                                catch
                                                {
                                                    _but.ForeColor = Color.FromArgb(10, 10, 10);
                                                }
                                            }
                                            #endregion

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.ResumeLayout(false);
        }
        #endregion

        private void ribbonButton1_Click(object sender, EventArgs e)
        {
            Close();
            Application.Exit();
        }

        #region Operações

        private void ribbonButton2_Click(object sender, EventArgs e)//Novo registo
        {
            GridContactos.CurrentCell = GridContactos.Rows[GridContactos.RowCount - 1].Cells[1];
            GridContactos.BeginEdit(false);
        }

        #region Apaga Registo
        private void ribbonButton3_Click(object sender, EventArgs e)
        {
            if (GridContactos.CurrentCell != null)
            {
                GridContactos.Rows.RemoveAt(GridContactos.CurrentRow.Index);
                GridContactos.EndEdit();
            }
        }
        #endregion

        #region Grava Registo
        private void ribbonButton4_Click(object sender, EventArgs e)
        {
            Validate();
            GridContactos.EndEdit();
            _daContactos.Update(_dtContactos);
        }
        #endregion

        #region Avança Registo

        private void btNext_Click(object sender, EventArgs e)
        {
            if (pnlTecnicos.Visible == false)
            {
                if (GridContactos.RowCount > 0 &&
                    GridContactos.CurrentRow.Index != GridContactos.RowCount - 1)
                {
                    GridContactos.CurrentCell = GridContactos[1, GridContactos.CurrentRow.Index + 1];
                }
            }
            else
            { }
        }
        #endregion

        private void btPrevious_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0 &&
               GridContactos.CurrentRow.Index != 0)
            { GridContactos.CurrentCell = GridContactos[1, GridContactos.CurrentRow.Index - 1]; }
        }

        private void btFirst_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0)
            { GridContactos.CurrentCell = GridContactos[1, 0]; }
        }

        private void btLast_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0)
            { GridContactos.CurrentCell = GridContactos[1, GridContactos.RowCount - 1]; }
        }

        #endregion

        private void btnExportaTXT_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0)
            {
                SaveFileDialog txtToSave = new SaveFileDialog
                                               {
                                                   Filter = "Ficheiro de Texto (*.txt)|*.txt"
                                               };
                txtToSave.ShowDialog();
                if (!string.IsNullOrEmpty(txtToSave.FileName))
                {
                    StreamWriter txt = new StreamWriter(txtToSave.FileName, false, System.Text.Encoding.Default);
                    foreach (DataGridViewColumn colname in GridContactos.Columns) txt.Write(colname.Name + ";");

                    txt.WriteLine();
                    foreach (DataGridViewRow linha in GridContactos.Rows)
                    {
                        foreach (DataGridViewCell coluna in linha.Cells)
                            txt.Write(coluna.Value + ";");
                        txt.WriteLine();
                    }
                    txt.Close();
                }
            }
        }

        private void tab1_Click(object sender, EventArgs e)
        {
            GridContactos.Visible = true;
            pnlTecnicos.Visible = false;
        }

        private void tab2_Click(object sender, EventArgs e)
        {
            GridContactos.Visible = false;
            pnlTecnicos.Visible = true;
        }

        private void txtNumero_TextChanged(object sender, EventArgs e)
        {
            _dvContactos.RowFilter = "[Telefone] like '%" + txtNumero.Text + "%'";
            status.Text = "Registos Encontrados: " + _dvContactos.Count;
        }

        private void pnlTecnicos_Enter(object sender, EventArgs e)
        {
            txtNome.DataBindings.Add("text", _dvTecnicos, "Nome");
        }


    }
}
